﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace RGG
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            panel_Welcome.Show();
            panel_Game.Hide();
            _rggManager = new RggManager(pictureBox_showGame, pictureBox_nextGB);
            _rggManagerTimer = new Timer { Interval = _rggManager.Config.Speed }; 
            _rggManagerTimer.Tick += delegate
            {
                pictureBox_showGame.Refresh();
                pictureBox_nextGB.Refresh();
                label_score.Text = (int.Parse(label_score.Text) + 1).ToString();
            };
            label_score.Text = "0";
            #region debug
            /*
#if DEBUG
            _rggManager.CurrentGeometricBody.BeforeKeyDown += delegate(object o, GbEventArgs args)
            {
                GeometricBody gb = o as GeometricBody;
                Debug.Assert(gb != null);
                using (
                    FileStream fs = new FileStream(DebugConfig.DebugTextPath + "DebugGbBeforeKeyDown.txt",
                        FileMode.Append, FileAccess.Write))
                {
                    DebugConfig.DrawNonZero(gb.LargeDotMatrix, fs);
                }
            };
#endif
            */
            #endregion

        }

        private void button_StartGame_Click(object sender, EventArgs e)
        {
            panel_Welcome.Hide();
            panel_Game.Visible = true;
            _rggManager.StartGame();
            _rggManagerTimer.Start();
            this.Focus();
        }
        
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Down)
            {
                while (_rggManager.KeyDown(e.KeyCode))
                {
                }
            }
            else
            {
                _rggManager.KeyDown(e.KeyCode);
            }
        }

        private void pictureBox_showGame_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            _rggManager.RedrawCurrentGb(g);
        }

        private void pictureBox_nextGB_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            _rggManager.RedrawNextGb(g);
        }
        
        private RggManager _rggManager;
        private Timer _rggManagerTimer;

        private void pictureBox_showGame_MouseDown(object sender, MouseEventArgs e)
        {
#if DEBUG
            _rggManager.ShowCurrentGbToolTip(toolTip1, pictureBox_showGame.PointToClient(MousePosition));
#endif
        }
    }
}